package api

import (
	"net/http"
	"strconv"

	"github.com/emicklei/go-restful"
	restfulspec "github.com/emicklei/go-restful-openapi"

	"github.com/xdhuxc/asset-management-system/src/model"
	"github.com/xdhuxc/asset-management-system/src/pkg"
)

type dictionaryController struct {
	*BaseController
}

func newDictionaryController(bc *BaseController) *dictionaryController {
	tags := []string{"ams-dictionary"}
	dc := &dictionaryController{bc}

	dc.ws.Route(dc.ws.GET("/dictionary/{groupID}").
		To(dc.Get).
		Doc("health check").
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Param(dc.ws.PathParameter("groupID", "the id of group").DataType("string").Required(true)).
		Returns(http.StatusOK, "OK", model.Result{}).
		Returns(http.StatusBadRequest, "ERROR", model.Result{}))

	return dc
}

func (dc *dictionaryController) Get(req *restful.Request, resp *restful.Response) {
	groupID, err := strconv.Atoi(req.PathParameter("groupID"))
	if err != nil {
		pkg.WriteResponse(resp, pkg.DictionaryError, err)
		return
	}

	result, err := dc.bs.DictionaryService.Get(groupID)
	if err != nil {
		pkg.WriteResponse(resp, pkg.DictionaryError, err)
		return
	}

	_ = resp.WriteEntity(model.NewResult(0, nil, result))
}
